<?php
declare (strict_types = 1);

namespace app\admin\controller\maichen;

use think\Request;
use think\facade\Request as Rq;
use think\facade\Session;
use app\admin\model\Operation as mo;
use app\common\controller\Backend;


class Operation extends Backend
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 获取运营组列表
     */
    public function groupList()
    {
        $group_leader_id = Session::get('admin.id');
        if (Rq::isPost()) {
            $model = new mo();
            $result = $model->getList();
            // dump($result);exit;
            if (is_array($result)) {
                $data = [
                    'code' => 0,
                    'msg' => '成功',
                    'data' => $result,
                ];
                return $data;
            }

        }
        return view('group_list');
    }

    public function groupAdd()
    {  
        if (Rq::isPost()) {
            $model = new mo();
            $dd = Rq::only(['name', 'appid', 'key']);
            $cr = $model->checkGroupByName($dd['name']);
            if ($cr->isEmpty())  {
                $dd['group_leader_id'] = Session::get('admin.id');
                $result = $model->save($dd);
                if ($result) {
                    $this->success('添加成功', url('maichen.Operation/groupList'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error('组名重复');
            }
        }
        return view();
    }

    public function groupDel()
    {
        $ids = Rq::post('ids');
        if (!empty($ids)) {
            $model = new mo();

            $model->delGroup($ids);
            $this->success(lang('delete success'));
        } else {
            $this->error(lang('supper man cannot delete'));

        }
    }

    public function groupEdit() {
        
        $id = Rq::get('id');
        $model = new mo();
        if (!empty($id)) {
            $result = $model->getInfoById($id);
            if (Rq::isPost()) {
                $moinfo = mo::find($id);
                $data = Rq::only(['name', 'appid', 'key']);
                $moinfo->name = $data['name'];
                $moinfo->appid = $data['appid'];
                $moinfo->key = $data['key'];
                $r = $moinfo->save();
                if ($r) {
                    $this->success('添加成功', url('maichen.Operation/groupList'));
                } else {
                    $this->error(lang('supper man cannot delete'));
                }
            }
           
            return view('group_edit', $result);
        }

        
    }
}
